package com.cmwa.solution.business.prdSetup.news.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.prdSetup.news.entity.NewsInfoCommonVo;
import com.cmwa.solution.business.prdSetup.news.entity.NewsInfoHotVo;
import com.cmwa.solution.business.prdSetup.news.entity.NewsType;
import com.cmwa.solution.business.prdSetup.news.service.NewsInfoService;
import com.cmwa.solution.sys.attach.entity.AttachVo;
import com.cmwa.solution.sys.attach.service.AttachService;
import com.cmwa.solution.sys.msg.util.StringUtil;
import com.google.common.base.Joiner;

import net.sf.json.JSONArray;

/**
 * 通用资讯信息表控制器
 * 
 * @author ex-hejm
 * @date 2019-01-21 14:59:01
 */
@Controller
@RequestMapping("/business/prdSetup/news")
public class NewsInfoController extends BaseController {

	private final static String JSP_PATH = "business/prdSetup/news/";
	@Resource
	private NewsInfoService newsInfoService;
	@Resource
	private AttachService attachService;
		
	/**
	 * 跳转到列表页面
	 * @author ex-hejm
	 * @date 2019-01-21 14:59:01
	 * @param model
	 * @return
	 */
	@RequestMapping("/newsInfoListView")
	public String newsInfoListView(ModelMap model) {
		return JSP_PATH + "newsInfoList";
	}
	/**
	 * 跳转到通用的新增页面
	 * @author ex-hejm
	 * @date 2019-01-21 14:59:01
	 * @param model
	 * @return
	 */
	@RequestMapping("/commonNewsInfoAddView")
	public String commonNewsInfoAddView(@RequestParam("newsType") String newsType, ModelMap model) {
		model.addAttribute("newsType", newsType);
		model.addAttribute("title", NewsType.get(newsType));
		return JSP_PATH + "commonNewsInfoAdd";
	}
	
	/**
	 * 跳转到通用的详情页面
	 * @author ex-hejm
	 * @date 2019-01-21 14:59:01
	 * @param model
	 * @return
	 */
	@RequestMapping("/commonNewsInfoDetailView")
	public String commonNewsInfoDetailView(@RequestParam("id") String id, ModelMap model) {
		NewsInfoCommonVo news = newsInfoService.getNewsInfoCommonById(id);
		model.addAttribute("news", news);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "NEWS_INFO");
		model.addAttribute("attachVos", attachVos);
		
		return JSP_PATH + "commonNewsInfoDetail";
	}
	
	/**
	 * 跳转到热点的详情页面
	 * @author ex-hejm
	 * @date 2019-01-21 14:59:01
	 * @param model
	 * @return
	 */
	@RequestMapping("/hotNewsInfoDetailView")
	public String hotNewsInfoDetailView(@RequestParam("id") String id, ModelMap model) {
		NewsInfoHotVo news = newsInfoService.getNewsInfoHotById(id);
		model.addAttribute("news", news);
		
		String archive2Json = "";
		//加载附件信息
		List<AttachVo> attaches = attachService.queryAttachByRelIdAndType(id, "HOT_NEWS");
		//如果附件信息不为空，则加载附件信息
		if(null != attaches) {
			archive2Json = StringUtil.changeSpecialCharacter(JSONArray.fromObject(attaches).toString());
		}
		model.addAttribute("attachList", archive2Json);
		
		return JSP_PATH + "hotNewsInfoDetail";
	}
	
	/**
	 * 跳转到热点产品新增页面
	 * @author ex-hejm
	 * @date 2019年1月23日上午10:36:51
	 * @param model
	 * @return
	 */
	@RequestMapping("/hotNewsInfoAddView")
	public String hotNewsInfoAddView(ModelMap model) {
		return JSP_PATH + "hotNewsInfoAdd";
	}

	@RequestMapping("/deleteCommonNews")
	@ResponseBody
	public Result<Object> deleteCommonNews(@RequestParam("id") String id) {
		Result<Object> result = new Result<Object>();
		newsInfoService.logicDeleteNewsInfoCommonById(id);
		return result;
	}
	
	/**
	 * 行业动态
	 * @author ex-hejm
	 * @date 2019年2月14日下午4:46:49
	 * @return
	 */
	@RequestMapping("/homeNews")
	@ResponseBody
	public List<NewsInfoCommonVo> homeNews() {
		List<NewsInfoCommonVo> list = newsInfoService.homeNews();
		return list;
	}
	
	
	@RequestMapping("/newsInfoCommonListPage")
	@ResponseBody
	public Page<NewsInfoCommonVo> newsInfoCommonListPage(SearchParam param) {
		Page<NewsInfoCommonVo> list = newsInfoService.newsInfoCommonListPage(param);
		return list;
	}
	
	@RequestMapping("/newsInfoHotListPage")
	@ResponseBody
	public Page<NewsInfoHotVo> newsInfoHotListPage(SearchParam param) {
		Page<NewsInfoHotVo> list = newsInfoService.newsInfoHotListPage(param);
		return list;
	}
	
	@RequestMapping("/insertNewsInfoCommon")
	public String insertNewsInfoCommon(NewsInfoCommonVo news, @RequestParam("attIds")String attIds) {
		newsInfoService.insertNewsInfoCommon(news, attIds);
		return redirectSuccess();
	}

	@RequestMapping("/insertNewsInfoHot")
	public String insertNewsInfoHot(NewsInfoHotVo news, @RequestParam("attIds")String attIds) {
		newsInfoService.insertNewsInfoHot(news, attIds);
		return redirectSuccess();
	}
	
	/**
	 * 热点产品详情页面-保存附件
	 * @author ex-hejm
	 * @date 2019年2月13日上午10:40:05
	 * @param news
	 * @return
	 */
	@RequestMapping("/saveHotNewsAttachs")
	public String saveHotNewsAttachs(NewsInfoHotVo news){
		String newsId = news.getId();
		if(StringUtils.isNotBlank(newsId)) {  // 保存附件关联关系表
			attachService.alterAttachRel(newsId, "HOT_NEWS", news.getAttIds());
		}
		return redirectSuccess();
	}
	
	/**
	 * 打包下载附件-热点
	 * @param id
	 * @throws IOException 
	 */
	@RequestMapping("/downloadHotNews")
	public void downloadHotNews(@RequestParam("id") String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
		NewsInfoHotVo news = newsInfoService.getNewsInfoHotById(id);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "HOT_NEWS");
		if (news != null && CollectionUtils.isNotEmpty(attachVos)) {
			List<String> attIds = new ArrayList<>();
			for (AttachVo attach : attachVos) {
				attIds.add(attach.getAttId());
			}
			//如无特殊需求，调用公共批量下载方法
			String fileName = news.getPrjName();
			attachService.downloadAttachZip(Joiner.on(",").join(attIds), fileName, request, response);
		}
	}
	/**
	 * 打包下载附件-通用
	 * @param id
	 * @throws IOException 
	 */
	@RequestMapping("/downloadCommonNews")
	public void downloadCommonNews(@RequestParam("id") String id, HttpServletResponse response, HttpServletRequest request) throws IOException {
		NewsInfoCommonVo news = newsInfoService.getNewsInfoCommonById(id);
		List<AttachVo> attachVos = attachService.queryAttachByRelIdAndType(id, "NEWS_INFO");
		if (news != null && CollectionUtils.isNotEmpty(attachVos)) {
			List<String> attIds = new ArrayList<>();
			for (AttachVo attach : attachVos) {
				attIds.add(attach.getAttId());
			}
			//如无特殊需求，调用公共批量下载方法
			String fileName = news.getNewsTitle();
			attachService.downloadAttachZip(Joiner.on(",").join(attIds), fileName, request, response);
		}
	}
}
